<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <base href="${base}/" />
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户注册 | 锦绣书城</title>
    <style>
        /* 全局样式 */
        :root {
            --primary-color: #8c7b68;
            --secondary-color: #3e2723;
            --light-bg: #f5efe6;
        }

        body.second {
            background: url('/images/book-bg.jpg') no-repeat center/cover;
            min-height: 100vh;
            font-family: 'Helvetica Neue', '宋体', sans-serif;
        }

        /* 注册容器 */
        .register-container {
            max-width: 800px;
            margin: 2rem auto;
            background: rgba(255,255,255,0.95);
            border-radius: 12px;
            box-shadow: 0 8px 32px rgba(62,39,35,0.1);
            overflow: hidden;
        }

        /* 表单样式 */
        .form-wrapper {
            padding: 2.5rem;
        }

        .form-title {
            font-size: 1.8rem;
            color: var(--secondary-color);
            border-left: 4px solid var(--primary-color);
            padding-left: 1rem;
            margin-bottom: 2rem;
        }

        .form-group {
            margin-bottom: 1.5rem;
            display: grid;
            grid-template-columns: 120px 1fr;
            align-items: center;
            gap: 1rem;
        }

        .form-control {
            padding: 0.8rem 1rem;
            border: 2px solid #d4c8b5;
            border-radius: 6px;
            transition: all 0.3s ease;
            width: 100%;
        }

        .form-control:focus {
            border-color: var(--primary-color);
            box-shadow: 0 4px 12px rgba(140,123,104,0.15);
        }

        /* 按钮样式 */
        .submit-btn {
            background: var(--primary-color);
            color: white;
            padding: 1rem 2rem;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            transition: transform 0.2s;
            width: 100%;
        }

        .submit-btn:hover {
            transform: translateY(-2px);
            opacity: 0.9;
        }

        /* 响应式适配 */
        @media (max-width: 768px) {
            .form-group {
                grid-template-columns: 1fr;
                gap: 0.5rem;
            }

            .form-wrapper {
                padding: 1.5rem;
            }
        }

        /* 错误提示 */
        .error-msg {
            color: #dc3545;
            font-size: 0.875rem;
            margin-top: 0.5rem;
            display: none;
        }

        /* 密码强度指示 */
        .password-strength {
            height: 4px;
            margin-top: 0.5rem;
            background: #eee;
            border-radius: 2px;
            position: relative;
        }

        .strength-bar {
            height: 100%;
            width: 0;
            transition: all 0.3s ease;
            border-radius: 2px;
        }

        /* 服务条款 */
        .agreement-box {
            max-height: 200px;
            overflow-y: auto;
            padding: 1rem;
            background: var(--light-bg);
            border: 1px solid #e8dfd3;
            border-radius: 6px;
            margin-top: 2rem;
        }
    </style>
    <script src="js/jquery-3.7.1.min.js"></script>
</head>
<body class="second">
<div class="register-container">
    <div class="form-wrapper">
        <h2 class="form-title">新用户注册</h2>
        ${msg}
        <form id="registerForm" action='/user/register' method="post" enctype="multipart/form-data">
            <div class="form-group">
                <label>账号：</label>
                <div>
                    <input class="form-control" name='account' id="registerAccount"
                           type="text" placeholder="2-20位字符（中英文/数字/下划线）">
                    <div class="error-msg" id="accountError"></div>
                </div>
            </div>

            <div class="form-group">
                <label>密码：</label>
                <div>
                    <input class="form-control" type="password" id="registerPassword"
                           name='password' placeholder="6-32位字符">
                    <div class="password-strength">
                        <div class="strength-bar"></div>
                    </div>
                </div>
            </div>

            <div class="form-group">
                <label>确认密码：</label>
                <div>
                    <input class="form-control" type="password" name='repassword'
                           id="confirmPassword" placeholder="请再次输入密码">
                    <div class="error-msg" id="confirmError"></div>
                </div>
            </div>

            <div class="form-group">
                <label>手机号码：</label>
                <div>
                    <input class="form-control" name='telphone' id="registerPhone"
                           type="tel" placeholder="11位手机号码">
                    <div class="error-msg" id="phoneError"></div>
                </div>
            </div>

            <div class="form-group">
                <label>电子邮箱：</label>
                <div>
                    <input class="form-control" name='email' id="registerEmail"
                           type="email" placeholder="请输入有效邮箱">
                    <div class="error-msg" id="emailError"></div>
                </div>
            </div>

            <div class="form-group">
                <label>用户头像：</label>
                <div>
                    <input class="form-control" name='avatar' id="avatarInput"
                           type="file" accept="image/*">
                    <div class="error-msg" id="avatarError"></div>
                </div>
            </div>

            <div class="form-group">
                <div></div>
                <button type="submit" class="submit-btn">立即注册</button>
            </div>
        </form>

        <div class="agreement-box">
            <!-- 原有服务条款内容保持不变 -->
        </div>
    </div>
</div>

<script>
    $(function() {
        // 实时表单验证
        $('#registerAccount').on('blur', validateUsername);
        $('#registerPassword').on('input', validatePassword);
        $('#confirmPassword').on('blur', validateConfirm);
        $('#registerPhone').on('blur', validatePhone);
        $('#registerEmail').on('blur', validateEmail);

        // 密码强度检测
        $('#registerPassword').on('input', function() {
            const strength = calculateStrength($(this).val());
            $('.strength-bar').css({
                'width': strength + '%',
                'background': getStrengthColor(strength)
            });
        });

        // 表单提交验证
        $('#registerForm').submit(function(e) {
            e.preventDefault();
            if(validateForm()) submitForm();
        });
    });

    function validateUsername() {
        const val = $('#registerAccount').val().trim();
        const regex = /^[\u4e00-\u9fa5a-zA-Z0-9_]{2,20}$/;
        if(!val) return showError('accountError', '账号不能为空');
        if(!regex.test(val)) return showError('accountError', '格式不符合要求');
        clearError('accountError');
        return true;
    }

    function validatePassword() {
        const val = $('#registerPassword').val();
        if(val.length < 6) return showError('passwordError', '密码至少6位');
        if(val.length > 32) return showError('passwordError', '密码过长');
        clearError('passwordError');
        return true;
    }

    function validateConfirm() {
        const pwd = $('#registerPassword').val();
        const confirm = $('#confirmPassword').val();
        if(pwd !== confirm) return showError('confirmError', '两次密码不一致');
        clearError('confirmError');
        return true;
    }

    function validatePhone() {
        const val = $('#registerPhone').val().trim();
        const regex = /^1[3-9]\d{9}$/;
        if(!regex.test(val)) return showError('phoneError', '手机号格式错误');
        clearError('phoneError');
        return true;
    }

    function validateEmail() {
        const val = $('#registerEmail').val().trim();
        const regex = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
        if(!regex.test(val)) return showError('emailError', '邮箱格式错误');
        clearError('emailError');
        return true;
    }

    function calculateStrength(password) {
        let strength = 0;
        if(password.length >= 8) strength += 25;
        if(/[A-Z]/.test(password)) strength += 25;
        if(/\d/.test(password)) strength += 25;
        if(/[^A-Za-z0-9]/.test(password)) strength += 25;
        return Math.min(strength, 100);
    }

    function getStrengthColor(strength) {
        if(strength < 40) return '#dc3545';
        if(strength < 70) return '#ffc107';
        return '#28a745';
    }

    function validateForm() {
        return validateUsername() &
            validatePassword() &
            validateConfirm() &
            validatePhone() &
            validateEmail();
    }

    function submitForm() {
        const formData = new FormData(document.getElementById('registerForm'));

        $('.submit-btn').prop('disabled', true).text('注册中...');

        $.ajax({
            url: '${pageContext.request.contextPath}/user/register',
            type: 'POST',
            data: formData,
            processData: false,
            contentType: false,
            success: function(response) {
                if(response === 'success') {
                    alert('注册成功！即将跳转登录页面');
                    window.location.href = "/user/login";
                } else {
                    alert('注册失败：' + response);
                }
            },
            error: function(xhr) {
                alert('系统错误：' + xhr.responseText);
            },
            complete: function() {
                $('.submit-btn').prop('disabled', false).text('立即注册');
            }
        });
    }

    function showError(id, msg) {
        $('#'+id).text(msg).show();
        return false;
    }

    function clearError(id) {
        $('#'+id).hide();
    }
</script>

<!-- 原有header和footer内容保持不变 -->

</body>
</html>